<?php

/*


  Action	$5	+1 Action

  Choose one; you get the version in parentheses:
  Each player gets +1 (+3) Cards;  or each player gains a Silver (Gold); or each player may trash a card from his hand and gain a card costing exactly $1 ($2) more.


 */

class Governor extends Card implements iAction, iKingdom {

    public function __construct() {
        parent::__construct("Governor", 5);
        $this->undoPlay = false;
        $this->undoBuy = true;
    }

    public function getDescription() {
        return array(
            "+1 Action",
            "Choose one; you get the version in parentheses: Each player gets +1 (+3) Cards;  or each player gains a Silver (Gold); or each player may trash a card from his hand and gain a card costing exactly $1 ($2) more.");
    }

    public function play(Game $game, $isRealCard) {

        $options = array(new Option(1, "+3 cards"), new Option(2, "Gain Gold"), new Option(3, "Trash card"));

        $output = MySocket::GetUserReply($game, Input_ChooseOne, $options, 1, 1, null, "Choose one");

        $answer = $output[0];
        $other_players = $game->getOtherPlayersNames();

        if ($answer == "Gain Gold") {
            $gold = $game->moveCardFromPileToPile("Gold", BoardCards, DiscardPile);
            ScreenDisplay::addScreenMessage($game, true, " gain " . nicePrint($gold), "gains " . ScreenDisplay::$LMSG);
            foreach ($other_players as $name) {
                $silver = $game->moveCardFromPileToPile("Silver", BoardCards, DiscardPile, $name);
                ScreenDisplay::addScreenMessage($game, true, " gain " . nicePrint($silver), "gains " . ScreenDisplay::$LMSG, $name);
            }
        } else if ($answer == "Trash card") {

            foreach ($other_players as $name) {
                $otherPlayHand = $game->getPlayHand($name);
                $otherPlayHand = getUniqueCards($otherPlayHand);
                if (sizeof($otherPlayHand) > 0) {
                    $otherPlayHand[] = new Option(1, "None");
                    $output = MySocket::GetUserReply($game, Input_ChooseOne, $otherPlayHand, 1, 1, null, "Choose card to trash", $name, true);
                }
            }

            $playHand = $game->getPlayHand();
            $playHand = getUniqueCards($playHand);
            if (sizeof($playHand) > 0) {
                $playHand[] = new Option(1, "None");
                $output = MySocket::GetUserReply($game, Input_ChooseOne, $playHand, 1, 1, null, "Choose card to trash");
                $old_card_name = $output[0];
                if ($old_card_name != "None") {
                    $old_card = $game->moveCardFromPileToPile($old_card_name, PlayHand, Trash);
                    ScreenDisplay::addScreenMessage($game, true, "trash " . nicePrint($old_card), "trashes " . ScreenDisplay::$LMSG);

                    $price = $old_card->getPrice($game);
                    $new_price = $price->add(2);
                    $optional_cards = $game->getBoardCardsByPrice($new_price, $new_price);
                    if (sizeof($optional_cards) > 0) {
                        $output = MySocket::GetUserReply($game, Input_ChooseOne, $optional_cards, 1, 1, null, "Choose a card to gain");

                        $new_card_name = $output[0];
                        $new_card = $game->moveCardFromPileToPile($new_card_name, BoardCards, DiscardPile);
                        ScreenDisplay::addScreenMessage($game, false, " and gain " . nicePrint($new_card), " and gains " . ScreenDisplay::$LMSG);
                    }
                }
            }
            $game->setPhase(Phase_Sleep);

            //the waiting function
            $users_replies = MySocket::getUsersReplies($game);
            foreach ($users_replies as $name => $output) {
                $game->setPhase(Phase_Sleep, $name);

                $old_card_name = $output[0];
                if ($old_card_name != "None") {
                    $old_card = $game->moveCardFromPileToPile($old_card_name, PlayHand, Trash, $name);
                    ScreenDisplay::addScreenMessage($game, true, "trash " . nicePrint($old_card), "trashes " . ScreenDisplay::$LMSG, $name);

                    $price = $old_card->getPrice($game);
                    $new_price = $price->add(1);
                    $optional_cards = $game->getBoardCardsByPrice($new_price, $new_price);
                    if (sizeof($optional_cards) > 0) {
                        MySocket::GetUserReply($game, Input_ChooseOne, $optional_cards, 1, 1, null, "Choose a card to gain", $name, true);
                    }
                }
            }

            //the waiting function
            $users_replies = MySocket::getUsersReplies($game);
            foreach ($users_replies as $name => $output) {
                $card_name = $output[0];
                $new_card = $game->moveCardFromPileToPile($card_name, BoardCards, DiscardPile, $name);
                ScreenDisplay::addScreenMessage($game, false, " and gain " . nicePrint($new_card), " and gains " . ScreenDisplay::$LMSG, $name);
                $game->setPhase(Phase_Sleep, $name);
            }
        } else {
            $drawed_cards = $game->drawCards(3);
            ScreenDisplay::addScreenMessage($game, true, " draw " . nicePrint($drawed_cards), "draws " . sizeof($drawed_cards) . " cards");
            foreach ($other_players as $name) {
                $drawed_cards = $game->drawCards(1, $name);
                ScreenDisplay::addScreenMessage($game, true, " draw " . nicePrint($drawed_cards), "draws " . sizeof($drawed_cards) . " cards", $name);
            }
        }

        $game->cardActionFinished();
        return true;
    }

}

?>